package com.wn.controller;

import com.wn.common.RedisConst;
import com.wn.common.enumeration.BusinessCode;
import com.wn.common.enumeration.MyAssert;
import com.wn.common.util.JwtUtil;
import com.wn.model.vo.CartVo;
import com.wn.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车接口")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private JwtUtil jwtUtil;  //要实现会话跟踪，从Token中获取用户id

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询购物车
     */
    @GetMapping("/getCart")
    @ApiOperation("查询购物车")
    public ResponseEntity<CartVo> getCart(@RequestHeader(name = "Authorization", required = false) String token) {

        //查询用户ID
        String userId = jwtUtil.getValue(token, "userId");
        log.info("用户ID:{}", userId);
        //调用业务层查询购物车
        CartVo cart = cartService.getCart(Long.valueOf(userId));
        log.info("购物车信息:{}", cart);
        return ResponseEntity.ok(cart);
    }

    /**
     * 添加商品到购物车
     */
    @PostMapping("/add/{bookId}")
    @ApiOperation("添加商品到购物车")
    public ResponseEntity<CartVo> addCartItem(
            @RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("bookId") Long bookId) {
        //获取用户ID
        String userId = jwtUtil.getValue(token, "userId");
        //调用业务层添加商品到购物车
        CartVo cart = cartService.addCartItem(Long.valueOf(userId), bookId);
        return ResponseEntity.ok(cart);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/delete/{bookId}")
    @ApiOperation("删除商品")
    public ResponseEntity<CartVo> removeCartItem(
            @RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("bookId") Long bookId) {
        //获取用户ID
        String userId = jwtUtil.getValue(token, "userId");
        //调用业务层删除商品
        CartVo cart = cartService.removeCartItem(Long.valueOf(userId), bookId);
        return ResponseEntity.ok(cart);
            }

    /**
     * 清空购物车
      */

    @DeleteMapping("/clear")
    @ApiOperation("清空购物车")
    public ResponseEntity<CartVo> clearCart(
            @RequestHeader(name = "Authorization", required = false) String token) {
        //获取用户ID
        String userId = jwtUtil.getValue(token, "userId");
        //调用业务层清空购物车
        CartVo cart = cartService.clearCart(Long.valueOf(userId)); 
        cart.clearCart();
        return ResponseEntity.ok(cart);
    }

    /**
     * 获取我的购物车
     */
    @GetMapping("/myCartDetails")
    @ApiOperation("获取我的购物车")
    public ResponseEntity<CartVo> myCartDetails(@RequestHeader(name = "Authorization", required = false) String token) {
        //获取用户ID
        String userId = jwtUtil.getValue(token, "userId");
        //从Redis中获取购物车的数据
        CartVo cart = (CartVo) redisTemplate.opsForValue().get(RedisConst.USER_CART_PREFIX + ":" + userId);
        return ResponseEntity.ok(cart);
    }
}

